﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Validation.Integration;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace Model.Test.Webforms
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {

                int? low;
                if (this.lowMarkTextBox.Text != string.Empty)
                    low = int.Parse(this.lowMarkTextBox.Text.Trim());
                else
                    low = null;

                int? high;
                if (this.highMarkTextBox.Text != string.Empty)
                    high = int.Parse(this.highMarkTextBox.Text.Trim());
                else
                    high = null;
                Customer cut = new Customer()
                {
                    FirstName = this.firstNameTextBox.Text.Trim(),
                    LastName = this.lastNameTextBox.Text.Trim(),
                    LowMark = low,
                    HighMark = high
                };
                Validator<Customer> customerValidator = ValidationFactory.CreateValidator<Customer>("RuleSetA");
                ValidationResults results = customerValidator.Validate(cut);
                if(results.IsValid==false)
                {
                    string errorlabel = string.Empty;
                    foreach (var item in results)
                    {
                        errorlabel += item.Message +"<br>";
                    }
                    this.CustomValidator1.IsValid = false;
                    this.CustomValidator1.ErrorMessage = errorlabel;
                }
            }
            else
            {
              
            }
        }
        protected void LastNameConvert(object sender , ValueConvertEventArgs e)
        {
            if (e.ValueToConvert.ToString() == string.Empty)
            {
                 e.ConvertedValue = null;
            }
        }
    }
}
